package java44th.group.service.wx.search;

import java44th.group.bean.po.*;
import java44th.group.bean.wx.vo.search.SearchVo;
import java44th.group.mapper.MarketGoodsMapper;
import java44th.group.mapper.MarketKeywordMapper;
import java44th.group.mapper.MarketSearchHistoryMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Aurhor: 徐云飞
 * @DATE: 2022/10/14 23:14
 * @Description:
 * @Version 1.0
 */
@Transactional
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    MarketKeywordMapper keywordMapper;
    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;


    //搜索主页
    @Override
    public SearchVo index() {
        //    热门关键字
        List<MarketKeyword> marketKeywords = keywordMapper.selectByExample(new MarketKeywordExample());
        //    第一个关键字

        MarketKeyword marketKeyword = marketKeywords.get(0);

        //    历史关键字
        Subject subject = SecurityUtils.getSubject();
        List<MarketSearchHistory> marketSearchHistorie = new ArrayList<>();
        if (subject.getPrincipals() != null) {
            MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            Integer id = primaryPrincipal.getId();

            MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();

            marketSearchHistoryExample.createCriteria().andUserIdEqualTo(id);

            List<MarketSearchHistory> marketSearchHistories = searchHistoryMapper.selectByExample(marketSearchHistoryExample);

            for (int i = marketSearchHistories.size()-1; i > 0; i--) {

                marketSearchHistorie.add(marketSearchHistories.get(i));

            }

        }

        return new SearchVo(marketKeyword, marketKeywords, marketSearchHistorie);

    }

    @Override
    public List helper(String keyword) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.createCriteria().andKeywordLike("%" + keyword + "%");

        List<MarketKeyword> marketKeywords = keywordMapper.selectByExample(marketKeywordExample);
        List<String> word = new ArrayList<>();
        for (MarketKeyword marketKeyword : marketKeywords) {

            word.add(marketKeyword.getKeyword());
        }


        return word;
    }

    @Override
    public void clearhistory() {

        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.createCriteria().andUserIdEqualTo(id);
        searchHistoryMapper.deleteByExample(marketSearchHistoryExample);


    }
}
